<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="js/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/iconfont.css">
    <link rel="stylesheet" href="css/main.css">
    <script src="js/jquery-1.12.4.min.js"></script>
    <script src="js/bootstrap/js/bootstrap.min.js"></script>
</head>

<body>
<div class="container-fluid">
    <div class="common_title">
        文章类别管理
    </div>
    <div class="container-fluid common_con">
        <table class="table table-striped table-bordered table-hover mp20 category_table">
            <thead>
            <tr>
                <th>名称</th>
                <th>Slug</th>
                <th class="text-center" width="100">操作</th>
            </tr>
            </thead>
            <tbody>

            </tbody>
            <tfoot>
            <tr>
                <td colspan="3" class="text-center">
                    <a href="#" class="btn btn-success btn-add" data-toggle="modal" data-target="#addModal">新增分类</a>
                </td>
            </tr>
            </tfoot>
        </table>
    </div>
</div>

<div class="modal fade" id="addModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"></button>
                <h4 class="modal-title">新增分类</h4>
            </div>
            <div class="modal-body">
                <form>
                    <div class="form-group">
                        <label for="recipient-name" class="control-label">分类名称:</label>
                        <input type="text" class="form-control cate_name" name="name">
                    </div>
                    <div class="form-group">
                        <label for="message-text" class="control-label">分类别名:</label>
                        <input type="text" class="form-control cate_slug" name="slug">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" id="model_shutoff">关闭</button>
                <button type="button" class="btn btn-primary" id="model_add">新增</button>
            </div>
        </div>
    </div>
</div>

<script>
  $('#addModal').modal({
    show: false,
    backdrop: false
  })

  $('#model_shutoff').click(function () {
    // alert('shutoff')
    $('#addModal').modal('hide')
  })

  $('#model_add').click(function () {
    // alert('add')
    $('#addModal').modal('hide')
  })

  $('.category_table').delegate('a', 'click', function () {
    if ($(this).hasClass('btn-info')) {
      $('#addModal .modal-title').html('修改分类名称')
      $('#addModal').modal('show')
    }
  })
</script>

<script src="./js/template-web.js"></script>
<script type="text/html" id="cate">
    {{each data}}
    <tr>
        <td>{{$value.name}}</td>
        <td>{{$value.slug}}</td>
        <td class="text-center">
            <a href="javascript:editTr({id:1,slug:uncategorized,&quot;name&quot;:&quot;未分类&quot;});"
               class="btn btn-info btn-xs">编辑</a>
            <a href="javascript:deleteTr( 1 );" class="btn btn-danger btn-xs">删除</a>
        </td>
    </tr>
    {{/each}}
</script>

<!--自己的逻辑-->
<script>
  /*
  思路:
  1.打开类别管理页面
  2.请求类别搜索接口,获取类别数据
      http://localhost:8000/admin/category_search
  3.编辑
      http://localhost:8000/admin/category_edit
  4.删除
      http://localhost:8000/admin/category_delete
  5.新增
      http://localhost:8000/admin/category_add
   */

  $(function () {
    //获取类别信息

    function getCate () {
      $.ajax({
        url: 'http://localhost:8000/admin/category_search',
        success: function (backData) {
          $('tbody').html(template('cate', backData))
        }
      })
    }


    getCate()


    //点击新增

    $('.btn-add').click(function () {
        $('.modal-title').text("肆意的修改吧!!!");
        $('#model_add').text("增加新的分类").removeClass('btn-success')
      //清楚历史内容
      $('form input').val('')
    })


    $('#model_add').click(function () {
        $.ajax({
          url:'http://localhost:8000/admin/category_add',
          type:'post',
          // data:$('form').serialize(),
          data:{
            name:$('.cate_name').val(),
            slug:$('.cate_slug').val()
          },
          success:function (backData) {
            console.log(backData)
            if(backData.code === 200){
              getCate()
            }
          }
        })
    })

  })
</script>

</body>

</html>



















